package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusServiceItem;
import cn.wolfcode.business.mapper.ServiceItemMapper;
import cn.wolfcode.business.service.ServiceItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ServiceItemServiceImpl implements ServiceItemService {
    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Override
    public List<BusServiceItem> list(BusServiceItem serviceItem) {
        return serviceItemMapper.selectList(serviceItem);
    }

    @Override
    public BusServiceItem getById(Long id) {
        return serviceItemMapper.selectById(id);
    }

    @Override
    public int save(BusServiceItem serviceItem) {
        if (serviceItem == null) {
            throw new RuntimeException("参数异常");
        }
        if (serviceItem.getName() == null || serviceItem.getOriginalPrice() == null ||
                serviceItem.getDiscountPrice() == null || serviceItem.getCarPackage() == null ||
                serviceItem.getServiceCatalog() == null || serviceItem.getInfo() == null) {
            throw new RuntimeException("参数异常");
        }
        if (serviceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) < 0 || serviceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) < 0 ||
                serviceItem.getOriginalPrice().compareTo(serviceItem.getDiscountPrice()) < 0) {
            throw new RuntimeException("原价不能小于折扣价");
        }
        if (BusServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        } else {
            serviceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setSaleStatus(BusServiceItem.SALESTATUS_OFF);
        serviceItem.setCreateTime(new Date());
        return serviceItemMapper.insert(serviceItem);
    }

    @Override
    public int update(BusServiceItem serviceItem) {
        if (serviceItem == null) {
            throw new RuntimeException("参数异常");
        }
        if (serviceItem.getName() == null || serviceItem.getOriginalPrice() == null ||
                serviceItem.getDiscountPrice() == null || serviceItem.getCarPackage() == null ||
                serviceItem.getServiceCatalog() == null || serviceItem.getInfo() == null) {
            throw new RuntimeException("参数异常");
        }
        if (serviceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) < 0 || serviceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) < 0 ||
                serviceItem.getOriginalPrice().compareTo(serviceItem.getDiscountPrice()) < 0) {
            throw new RuntimeException("原价不能小于折扣价");
        }
        BusServiceItem old = serviceItemMapper.selectById(serviceItem.getId());
        if (old == null || old.getCarPackage() == null || old.getAuditStatus() == null) {
            throw new RuntimeException("禁止修改套餐");
        }
        if (!old.getCarPackage().equals(serviceItem.getCarPackage())) {
            throw new RuntimeException("禁止修改套餐");
        }
        if (!old.getSaleStatus().equals(serviceItem.getSaleStatus()) || old.getSaleStatus().equals(BusServiceItem.SALESTATUS_ON)) {
            throw new RuntimeException("上架状态禁止修改");
        }
        if (BusServiceItem.CARPACKAGE_YES.equals(old.getCarPackage()) && BusServiceItem.AUDITSTATUS_AUDITING.equals(old.getAuditStatus())) {
            throw new RuntimeException("审批中禁止修改");
        }
        if (BusServiceItem.CARPACKAGE_YES.equals(old.getCarPackage()) && (BusServiceItem.AUDITSTATUS_APPROVED.equals(old.getAuditStatus()) ||
                BusServiceItem.AUDITSTATUS_REPLY.equals(old.getAuditStatus()))) {
            serviceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        }
        return serviceItemMapper.updateById(serviceItem);
    }

    @Override
    public int saleOnHandle(Long id) {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        BusServiceItem serviceItem = serviceItemMapper.selectById(id);
        if (serviceItem == null) {
            throw new RuntimeException("服务项不存在");
        }
        if (BusServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())) {
            throw new RuntimeException("服务项已上架");
        }
        if (BusServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())
                && !BusServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            throw new RuntimeException("服务项未审核通过");
        }
        return serviceItemMapper.change(BusServiceItem.SALESTATUS_ON, id);
    }

    @Override
    public int saleDownHandle(Long id) {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        BusServiceItem serviceItem = serviceItemMapper.selectById(id);
        if (serviceItem == null) {
            throw new RuntimeException("服务项不存在");
        }
        if (BusServiceItem.SALESTATUS_OFF.equals(serviceItem.getSaleStatus())) {
            throw new RuntimeException("服务项已下架,请勿重复操作");
        }
        return serviceItemMapper.change(BusServiceItem.SALESTATUS_OFF, id);
    }
}
